<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/25
 * Time: 15:46
 */
// 允许跨域请求
header("Access-Control-Allow-Origin:*");
// 屏蔽错误警告
//ini_set("error_reporting","E_ALL & ~E_NOTICE");

require_once('dbServerConnect.php');
$con = new DB();
$link = $con->mySqlServer();

if (!$link) {
    echo '数据库连接失败';
    return;
}


$tag = ""; $query = ""; $pageNum = 0; $pageSize = 1;

$tag = $_GET['tag'];
$query = $_GET['query'];
$pageNum = $_GET['pagenum'];
$pageSize = $_GET['pagesize'];


//if(isset($_GET['pagenum']) || isset($_GET['pagesize'])) {
//    if(!empty($_GET['pagenum']) && !empty($_GET['pagesize'])) {
//
//        if(is_numeric($_GET['pagenum']) && is_numeric($_GET['pagesize'])) {
//            $pageNum = $_GET['pagenum'];
//            $pageSize = $_GET['pagesize'];
//        }
//        else {
//            echo "请传入正确参数(pagenum、pagesize)必须为整形数字";
//            return;
//        }
//    }
//    else {
//        echo "pagenum、pagesize参数不能为空";
//        return;
//    }
//}
//else if(!isset($_GET['pagenum']) || !isset($_GET['pagesize'])) {
//    echo "请求体缺少必要参数-->pagenum，pagesize";
//    return;
//}


$arrayList = array(array('id'=>'','title'=>'','description'=>'','container'=>'','createdate'=>'','views'=>'','istop'=>'','isHide'=>'','total'=>''));
if($query == "" && $tag == "") {
    if($pageNum != 0) {
        $pageNum = ($pageNum - 1) * $pageSize;
    }
    // $sql = "select * from blog_container ORDER BY createdate DESC limit $pageNum, $pageSize";
    // 先把顶置(istop)分组.并倒序排序,然后再根据帖子创建时间倒序排序
    $sql = "SELECT * FROM blog_container ORDER BY istop DESC , createdate desc";
    $result = mysqli_query($link,$sql) or die(mysqli_error($link));
    // 获取总行数
    $total = mysqli_num_rows(mysqli_query($link,"select * from blog_container"));
    $res = mysqli_fetch_all($result);
    // 截取数组指定行数
    $res = array_slice($res, $pageNum, $pageSize);
    if ($res) {
        echo getBlogContainer($res,$total);
    }
    else {
        echo false;
    }
}
// 查询搜索
else if(!empty($query)) {
    if($pageNum != 0) {
        $pageNum = ($pageNum - 1) * $pageSize;
    }
    $sql = "select * from blog_container where title like '%$query%' ORDER BY createdate DESC limit $pageNum, $pageSize";
    $result = mysqli_query($link,$sql);
    $total = mysqli_num_rows(mysqli_query($link,"select * from blog_container where title like '%$query%'"));
    $res = mysqli_fetch_all($result);
    if ($res) {
        echo getBlogContainer($res,$total);
    }
    else {
        echo json_encode($arrayList);
    }
}
// 根据分类获取
else {
    if($pageNum != 0) {
        $pageNum = ($pageNum - 1) * $pageSize;
    }
    $sql = "select * from blog_container where tag='$tag' ORDER BY createdate DESC limit $pageNum, $pageSize";
    $result = mysqli_query($link,$sql);
    $total = mysqli_num_rows(mysqli_query($link,"select * from blog_container where tag='$tag'"));
    $res = mysqli_fetch_all($result);
    if ($res) {
        echo getBlogContainer($res,$total);
    }
    else {
        echo json_encode($arrayList);
    }
}



function getBlogContainer($res,$total) {
    for ($i = 0;$i < sizeof($res); $i++) {
        $arrayList[$i]['id'] = $res[$i][0];
        $arrayList[$i]['author'] = $res[$i][1];
        $arrayList[$i]['title'] = $res[$i][2];
        $arrayList[$i]['description'] = $res[$i][3];
        $arrayList[$i]['container'] = $res[$i][4];
        $arrayList[$i]['createdate'] = $res[$i][5];
        $arrayList[$i]['tag'] = $res[$i][6];
		$arrayList[$i]['lastdate'] = $res[$i][7];
		$arrayList[$i]['views'] = $res[$i][8];
        $arrayList[$i]['istop'] = $res[$i][9];
        $arrayList[$i]['isHide'] = $res[$i][10];
    }
    $arrayList[0]['total'] = $total;
    return json_encode($arrayList);
}